package com.hk.kindheartedness;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.hk.kindheartedness.db.DBOpenHelper;


/**
 * 患者查看科室详情
 */
public class PatientDepartmentsInfoActivity extends AppCompatActivity {

    //    Initialize Toast
    private Toast toast;

    //    Initialize intent
    private Intent intent;

    //    声明DBOpenHelper对象
    private DBOpenHelper dbOpenHelper;

    private TextView departmentsName,describe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_departments_info);
//        实例化
        dbOpenHelper = new DBOpenHelper(PatientDepartmentsInfoActivity.this, "db_kindheartedness", null, 1);
//        获取文本框控制权
        departmentsName = findViewById(R.id.departmentsName);
        describe = findViewById(R.id.describe);
//        获取科室ID
        Intent intent2 = getIntent();
        Bundle bundle = intent2.getExtras();
        String departmentsId = (String) bundle.get("departmentsId");
//        通过科室ID查询科室名称和简介
        findByDepartmentsId(departmentsId);



    }

    /**
     * 通过科室ID查询科室名称和简介
     * @param departmentsId 科室ID
     */
    private void findByDepartmentsId(String departmentsId) {
        SQLiteDatabase readableDatabase = dbOpenHelper.getReadableDatabase();
        Cursor queryDepartments = readableDatabase.query("departments", null, "id = ?", new String[]{departmentsId}, null, null, null, null);
        while (queryDepartments.moveToNext()) {
            String departmentsName2 = queryDepartments.getString(1);
            String describe2 = queryDepartments.getString(2);
            departmentsName.setText(departmentsName2);
            describe.setText("        "+"暂无简介");
            if (describe2 != null) {
                describe.setText("        "+describe2);
            }
        }

    }

    /**
     * 断开数据库连接
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbOpenHelper != null) {
            dbOpenHelper.close();
        }
    }

}